﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.Security;
using System.Collections.ObjectModel;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class RoleManagerSectionManager : ConfigurationManagerBase
	{
		private RoleManagerSection section;

		public RoleManagerSectionManager()
		{
			this.section = (RoleManagerSection)base.GetConfigurationSection("system.web/roleManager");
		}

		public void Modify(Boolean cacheRolesInCookie, String cookieName, String cookiePath, CookieProtection cookieProtection, Boolean cookieRequireSSL, Boolean cookieSlidingExpiration, TimeSpan cookieTimeout, Boolean createPersistentCookie, String defaultProvider, String domain, Boolean enabled, Int32 maxCachedResults)
		{
			if (String.IsNullOrEmpty(cookieName))
			{
				cookieName = ".ASPXROLES";
			}
			if (String.IsNullOrEmpty(cookiePath))
			{
				cookiePath = "/";
			}
			if (String.IsNullOrEmpty(defaultProvider))
			{
				defaultProvider = "AspNetSqlRoleProvider";
			}
			if (domain == null)
			{
				domain = "";
			}
			if (maxCachedResults < 0)
			{
				maxCachedResults = 25;
			}
			this.section.CacheRolesInCookie = cacheRolesInCookie;
			this.section.CookieName = cookieName;
			this.section.CookiePath = cookiePath;
			this.section.CookieProtection = cookieProtection;
			this.section.CookieRequireSSL = cookieRequireSSL;
			this.section.CookieSlidingExpiration = cookieSlidingExpiration;
			this.section.CookieTimeout = cookieTimeout;
			this.section.CreatePersistentCookie = createPersistentCookie;
			this.section.DefaultProvider = defaultProvider;
			this.section.Domain = domain;
			this.section.Enabled = enabled;
			this.section.MaxCachedResults = maxCachedResults;
			this.Save();
		}
		public RoleManagerSection GetRoleManager()
		{
			return this.section;
		}

		public void AddProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Add(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ModifyProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(RequiredConnectionStringNameProviderSettings provider)
		{
			RequiredConnectionStringNameProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public RequiredConnectionStringNameProviderSettings GetProvider(string name)
		{
			return RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<RequiredConnectionStringNameProviderSettings> GetProviders()
		{
			Collection<RequiredConnectionStringNameProviderSettings> roleProviders = new Collection<RequiredConnectionStringNameProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				roleProviders.Add(
					RequiredConnectionStringNameProviderSettings.ConvertToRequiredConnectionStringNameProviderSettings(provider)
				);
			}

			return roleProviders;
		}
	}

	public sealed class RoleManagerSectionManagerForSql : ConfigurationManagerBase
	{
		private RoleManagerSection section;

		public RoleManagerSectionManagerForSql()
		{
			this.section = (RoleManagerSection)base.GetConfigurationSection("system.web/roleManager");
		}

		public void AddProvider(SqlRoleProviderSettings provider)
		{
			SqlRoleProviderSettings.Add(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ModifyProvider(SqlRoleProviderSettings provider)
		{
			SqlRoleProviderSettings.Modify(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void RemoveProvider(SqlRoleProviderSettings provider)
		{
			SqlRoleProviderSettings.Remove(
				this.section.Providers,
				provider
			);
			this.Save();
		}
		public void ClearProviders()
		{
			this.section.Providers.Clear();

			this.Save();
		}
		public SqlRoleProviderSettings GetProvider(string name)
		{
			return SqlRoleProviderSettings.ConvertToSqlRoleProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<SqlRoleProviderSettings> GetProviders()
		{
			Collection<SqlRoleProviderSettings> providers = new Collection<SqlRoleProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				providers.Add(
					SqlRoleProviderSettings.ConvertToSqlRoleProviderSettings(provider)
				);
			}

			return providers;
		}
	}
}